package com.core.website.service.impl;

import com.core.website.entity.Organization;
import com.core.website.entity.OrganizationUserCascadeDto;
import com.core.website.mapper.OrganizationMapper;
import com.core.website.service.OrganizationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class OrganizationServiceImpl implements OrganizationService {
    @Autowired
    private OrganizationMapper organizationMapper;

    @Override
    public int add(Organization organization) {
        organization.setId(UUID.randomUUID().toString().toUpperCase().replace("-", ""));
        return organizationMapper.add(organization);
    }

    @Override
    public int edit(Organization organization) {
        return organizationMapper.edit(organization);
    }

    @Override
    public int delete(List<String> ids) {
        if(null!=ids&&ids.size()>0){
            for(String id:ids){
                Organization organization=new Organization();
                organization.setParentId(id);
                List<Organization> organizations = organizationMapper.queryAll(organization);
                if(organizations.size()>0){
                    return -1;
                }
            }
        }

        return organizationMapper.delete(ids);
    }
    @Override
    public String checkUse(List<String> ids) {
        String result=null;
        List<String> names=new ArrayList<>();
        if(null!=ids&&ids.size()>0){
            for(String id:ids){
                int i=organizationMapper.checkUse(id);
                if(i>0){
                    Organization organization = organizationMapper.queryById(id);
                    names.add(organization.getName());
                }

            }
        }
        if(names.size()>0){
            result=StringUtils.join(names,",");
        }
        return result;
    }

    @Override
        public List<Organization> queryAll(Organization organization) {
        List<Organization> result=organizationMapper.queryAll(organization);
        return result;
    }
    @Override
    public List<Organization> queryStatistic() {
        List<Organization> result=organizationMapper.selectStatistic();
        return result;
    }

    @Override
    public Organization queryOne(String id) {
        Organization organization=organizationMapper.queryById(id);
        return organization;
    }

    @Override
    public List<OrganizationUserCascadeDto> queryCascadeUser(String userId) {
        Map map = new HashMap();
        map.put("userId",userId);
        return organizationMapper.queryCascadeUser(map);
    }
}
